package com.netcross.myapplication;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import com.netcross.myapplication.databinding.FragmentFirstBinding;

import java.util.Timer;
import java.util.TimerTask;

public class FirstFragment extends Fragment {

private FragmentFirstBinding binding;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

      binding = FragmentFirstBinding.inflate(inflater, container, false);
      return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button button = (Button) getActivity().findViewById(R.id.button_testDialog);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                		final ProgressDialog dialog = new ProgressDialog(getActivity());
//        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//        dialog.setMessage("正在加载中");
//        dialog.setMax(100);
//        final Timer timer = new Timer();
//        timer.schedule(new TimerTask() {
//            int progress = 0;
//
//            public void run() {
//                dialog.setProgress(progress += 5);
//                if (progress == 100) {
//                    timer.cancel();
//                }
//            }
//        }, 0, 1000);
//        dialog.show();

//正在加载中
//                ProgressDialog dialog = new ProgressDialog(getActivity());
//                dialog.setMessage("正在加载中");
//                dialog.show();
                View view1 = getLayoutInflater().inflate(R.layout.dialog,null);

                 AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle("搜索")//设置对话框的标题
                          .setView(view1)
                //设置对话框的按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "点击了取消按钮", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("查询", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "点击了查询的按钮", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();

            }
        });
        binding.buttonFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavHostFragment.findNavController(FirstFragment.this)
                        .navigate(R.id.action_FirstFragment_to_SecondFragment);
            }
        });
    }


@Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}